extends Node2D

signal summon

@onready var com1 = $"1"
@onready var com2 = $"2"
@onready var com3 = $"3"

var Awakening = false # 是否觉醒
var upgrade = false

# 简化后的冷却机制
var cool_down_done: bool = true
var cool_down_time = 0.25

#预加载弹幕场景
var bullet: PackedScene = preload("res://Scenes/core_attack.tscn")

var summon_count = 0

func _ready() -> void:
	global_position = Global.core_position
	com1.visible = false
	com2.visible = false
	com3.visible = false
	summon.connect(self._summon_count_changed)
	Global.mouse_left.connect(self.shoot)

func _physics_process(delta: float) -> void:
	self.rotate(20 * delta)

func _summon_count_changed() -> void:
	if summon_count < 3:
		summon_count += 1
		match summon_count:
			1:
				com1.visible = true
				com1.position = Vector2(0, -96)
		
			2:
				com1.visible = true
				com2.visible = true
				com2.position = Vector2(0, 96)

			3:
				com1.visible = true
				com2.visible = true
				com3.visible = true
				com2.position = Vector2(83.04, 48)
				com3.position = Vector2(-83.04, 48)
	elif summon_count == 3 && upgrade == false:
		upgrade = true
		cool_down_time = 0.1

			
	elif summon_count == 3 && upgrade == true:
		Awakening = true
		

func shoot() -> void:
	if cool_down_done:
		for child in self.get_children():
			if child.visible:
				var b = bullet.instantiate()
				child.add_child(b)
				b.global_position = child.global_position
				b._spawn_ready()
		start_cooldown()

func start_cooldown() -> void:
	cool_down_done = false
	await get_tree().create_timer(cool_down_time).timeout
	cool_down_done = true
